# module giving several math function for view objects
from market.models import Asset
from market.view import View
from market.math import vectorScalarProduct
from market.math import vectorAdd

###########################################################################################
# from the DB we are sure we got all assets (some assets can be missing in view).
def getAllAssets():
	return Asset.objects.all()

###########################################################################################
# this function transform a view to a vector, following a specific asset order.
def view2vec(v, assetOrder):
	return [v.getViewOnAsset(a) for a in assetOrder]

###########################################################################################
# assigns the probalility to the asset in the order they are specified.
def vec2view(v, assetOrder):
	return View(assetOrder, v)

###########################################################################################
# return the index of the specified asset in the list.
def indexOfAssetInList(asset, assetList):
	for a in range(len(assetList)):
		if( assetList[a].name == asset ):
			return a
	return -1

###########################################################################################
# performs the standard scalar prodcuts taking care of ways the assets are listed in both view
def viewScalarProduct(v1, v2): 
	lAssets = getAllAssets()
	vec1 = view2vec(v1, lAssets)
	vec2 = view2vec(v2, lAssets)
	return vectorScalarProduct(vec1, vec2)

###########################################################################################
# adds values taking care of ways the assets are listed in both view
def viewAdd(v1,v2):
	lAssets = getAllAssets()
	vec1 = view2vec(v1, lAssets)
	vec2 = view2vec(v2, lAssets)
	return vec2view(vectorAdd(vec1, vec2), lAssets)

###########################################################################################
# multiplication of the view with a scalar
def viewMul(v, scalar):
	lAssets = getAllAssets()
	vec = view2vec(v, lAssets)
	vec = [scalar * elt for elt in vec]
	return vec2view(vec, lAssets)

